package com.yanqu.road.server.cmd.activity.celebration;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.celebration.CelebrationProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.celebration.CelebrationMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * @Description 庆典使用道具
 * @Author cwq
 * @Data 2021/7/12 16:08
 */
@Cmd(code = GameProtocol.S_CELEBRATION_SYN_LV_CROSS_TO_GAME, desc = "同步庆典建筑等级 跨服返回区服 协议")
public class CelebrationSynLvFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CelebrationProto.CelebrationSynLvCrossToGameMsg crossToGameMsg = CelebrationProto.CelebrationSynLvCrossToGameMsg.parseFrom(packet.getBytes());
        synchronized (CelebrationSynLvFromCrossCmd.class) {
            long oldLevel = CelebrationMgr.getActivityBuildLv().longValue();
            if (crossToGameMsg.getCurLv() != CelebrationMgr.getActivityBuildLv().longValue()) {
                // 同步所有的成员
                List<GamePlayer> playerList = GamePlayerMgr.getAllOnlinePlayer();
                for (GamePlayer player : playerList) {
                    player.notifyListener(eGamePlayerEventType.CelebrationActivityFestiveCrossTotalValue.getValue(), crossToGameMsg.getCurLv());
                }
                CelebrationMgr.setActivityBuildLv(crossToGameMsg.getCurLv());
                CelebrationMgr.getLogger().info("周年庆典收到建筑等级同步。receiveLevel:{},oldLevel:{},newLevel:{}", crossToGameMsg.getCurLv(), oldLevel, CelebrationMgr.getActivityBuildLv());
            } else {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(packet.getPlayerId());
                if (player != null) {
                    player.notifyListener(eGamePlayerEventType.CelebrationActivityFestiveCrossTotalValue.getValue(), crossToGameMsg.getCurLv());
                }
            }
        }
    }
}
